package com.zjhd.sealcutting.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
@ApiModel(description = "搜索结果")
public class SearchResult<T> {
    @ApiModelProperty(value = "页大小")
    private int pageSize;
    @ApiModelProperty(value = "页码")
    private int current;
    @ApiModelProperty(value = "总数")
    private long totalCount;
    @ApiModelProperty(value = "总页数")
    private long pages;
    @ApiModelProperty(value = "数据")
    private List<T> pagedItems;

    public SearchResult() {
    }

    private SearchResult(Builder builder) {
        this.pageSize = builder.pageSize;
        this.current = builder.current;
        this.totalCount = builder.totalCount;
        this.pagedItems = builder.pagedItems;
    }

    public long getPages() {
        long pages = 0L;
        try {
            pages = (totalCount + pageSize - 1) / pageSize;
        } catch (Exception e) {
            return pages;
        }
        return pages;
    }

    public static class Builder<T> {
        private int pageSize;
        private int current;
        private long totalCount;
        private long pages;
        private List<T> pagedItems;

        public Builder() {
        }

        public Builder setPageSize(int pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder setCurrent(int current) {
            this.current = current;
            return this;
        }

        public Builder setTotalCount(long totalCount) {
            this.totalCount = totalCount;
            return this;
        }

        public Builder setPagedItems(List<T> pagedItems) {
            this.pagedItems = pagedItems;
            return this;
        }

        public SearchResult build() {
            return new SearchResult<T>(this);
        }
    }
}
